package com.heimali.sf.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;

import com.heimali.sf.HomeActivity;
import com.heimali.sf.R;
import com.heimali.sf.SiFaApplication;
import com.heimali.sf.config.Constants;
import com.heimali.sf.utils.AndroidUtils;

public class BaseFragment extends Fragment {
	public static final String TAG = "BaseFragment";
	public static long lastExitTime = 0;

	protected double defaultPropertyFee = Double.valueOf(Constants.DEFAULT_PROPERTY_FEE);
	protected double defaultDamagesFee = Double.valueOf(Constants.DEFAULT_DAMAGES_FEE);
	protected double defaultDivorceFee = Double.valueOf(Constants.DEFAULT_DIVORCE_FEE);

	protected double rebate = Constants.DEFAULT_REBATE;

	protected HomeActivity activity;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		activity = (HomeActivity) getActivity();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return false;
	}

	/**
	 * 退出应用
	 */
	public void exitApp() {
		long curTime = System.currentTimeMillis();
		if (curTime - lastExitTime > 2000) {
			lastExitTime = curTime;
			AndroidUtils.toast(activity, R.string.back_confirm);
			return;
		}

		SiFaApplication.exit();
	}

	/**
	 * 保留2为小数
	 * 
	 * @param in
	 * @return
	 */
	protected double formatF2(double in) {
		return (double) (Math.round(in * 100) / 100.0);
	}

	protected double calcPropertyOdd(double sum) {
		double r = 0;
		if (sum < 0) {
			r = 0;
		} else if (sum <= 10000) {
			r = 50 * 100;
		} else if (sum <= 100000) {
			r = (sum - 10000) * 2.5;
		} else if (sum <= 200000) {
			r = (sum - 100000) * 2;
		} else if (sum <= 500000) {
			r = (sum - 200000) * 1.5;
		} else if (sum <= 1000000) {
			r = (sum - 500000) * 1;
		} else if (sum <= 2000000) {
			r = (sum - 1000000) * 0.9;
		} else if (sum <= 5000000) {
			r = (sum - 2000000) * 0.8;
		} else if (sum <= 10000000) {
			r = (sum - 5000000) * 0.7;
		} else if (sum <= 20000000) {
			r = (sum - 10000000) * 0.6;
		} else {
			r = (sum - 20000000) * 0.5;
		}
		return r / 100;
	}

	/**
	 * 财产案件
	 * 
	 * @param sum
	 * @return
	 */
	protected double calcProperty(double sum) {
		double L0 = 0, L1 = 50, L2 = L1 + calcPropertyOdd(100000), L3 = L2 + calcPropertyOdd(200000), L4 = L3
				+ calcPropertyOdd(500000), L5 = L4 + calcPropertyOdd(1000000), L6 = L5 + calcPropertyOdd(2000000), L7 = L6
				+ calcPropertyOdd(5000000), L8 = L7 + calcPropertyOdd(10000000), L9 = L8 + calcPropertyOdd(20000000);

		if (sum < 0) {
			return L0;
		} else if (sum <= 10000) {
			return L1;
		} else if (sum <= 100000) {
			return L1 + calcPropertyOdd(sum);
		} else if (sum <= 200000) {
			return L2 + calcPropertyOdd(sum);
		} else if (sum <= 500000) {
			return L3 + calcPropertyOdd(sum);
		} else if (sum <= 1000000) {
			return L4 + calcPropertyOdd(sum);
		} else if (sum <= 2000000) {
			return L5 + calcPropertyOdd(sum);
		} else if (sum <= 5000000) {
			return L6 + calcPropertyOdd(sum);
		} else if (sum <= 10000000) {
			return L7 + calcPropertyOdd(sum);
		} else if (sum <= 20000000) {
			return L8 + calcPropertyOdd(sum);
		} else {
			return L9 + calcPropertyOdd(sum);
		}
	}

}
